package com.artisan.web.action;

import com.artisan.common.pojo.PageBean;
import com.artisan.common.pojo.PageParam;
import com.artisan.commonweb.result.ActionResult;
import com.artisan.medium.admin.service.BusinessService;
import com.artisan.model.Business;
import com.artisan.model.BusinessType;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yang on 2017/7/2.
 */
@Controller
@RequestMapping("/admin/bus/")
public class BusinessController {
    @Autowired
    private BusinessService businessService;
    @RequestMapping("business_list")
    public ModelAndView loginView(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Integer> result = new HashMap<>();
        return new ModelAndView("business/business_list", result);
    }

    @RequestMapping("add_business")
    public ModelAndView add_business(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Integer> result = new HashMap<>();
        return new ModelAndView("business/add_business", result);
    }

    @RequestMapping("edit_business")
    public ModelAndView edit_business(HttpServletRequest request, HttpServletResponse response) {
        int id=Integer.valueOf(String.valueOf(request.getParameter("id")));
        Map<String, Object> result = new HashMap<String,Object>();
        Business business= businessService.getBusinessById(id);
        result.put("bus",business);
        return new ModelAndView("business/edit_business", result);
    }

    @ResponseBody
    @RequestMapping(value = "business_select", method = RequestMethod.GET)
    public PageBean business_select(HttpServletRequest request, Integer limit, Integer pageNumber, int offset, int pid, String searchKey) throws Exception {
        Map<String, Object> paramMap = new HashMap<String, Object>(); // 业务条件查询参数
        paramMap.put("searchKey", searchKey);
        paramMap.put("pid", pid);
        return businessService.listPage(new PageParam(offset / limit + 1, limit), paramMap);
    }

    @ResponseBody
    @RequestMapping(value = "delBusiness" , method = RequestMethod.GET)
    public ActionResult delType(String ids){
        ActionResult actionResult=new ActionResult();
        if(businessService.delBusiness(ids)>0){
            actionResult.setCode(200);
        }else{
            actionResult.setCode(500);
        }
        return  actionResult;
    }
    @RequestMapping("add_bus")
    public String add_bus(HttpServletRequest request, HttpServletResponse response, Business business, @Param(value = "imgfile") final MultipartFile imgfile) throws Exception {
        if (imgfile != null) {
            String path = request.getSession().getServletContext().getRealPath("/upload/bus/");
            File file = new File(path);
            if (!file.exists()) {
                file.mkdirs();
            }
            String fileName = imgfile.getOriginalFilename();
            //文件命名
            fileName = System.currentTimeMillis() + fileName.substring(fileName.lastIndexOf("."));
            File newFile = new File(path, fileName);
            imgfile.transferTo(newFile);
            business.setLogo(fileName);
            System.out.println(businessService.insert(business));
        }
        return "redirect:/admin/bus/business_list.do";

    }

    @RequestMapping("update_bus")
    public String update_bus(HttpServletRequest request, HttpServletResponse response, Business business, @Param(value = "imgfile") final MultipartFile imgfile) throws Exception {
        if (imgfile != null) {
            String path = request.getSession().getServletContext().getRealPath("/upload/bus/");
            File file = new File(path);
            if (!file.exists()) {
                file.mkdirs();
            }
            String fileName = imgfile.getOriginalFilename();
            //文件命名
            fileName = System.currentTimeMillis() + fileName.substring(fileName.lastIndexOf("."));
            File newFile = new File(path, fileName);
            imgfile.transferTo(newFile);
            business.setLogo(fileName);
            businessService.update(business);
        }
        return "redirect:/admin/bus/business_list.do";

    }

    @ResponseBody
    @RequestMapping(value = "get_three_type", method = RequestMethod.GET)
    public ActionResult get_three_type(int pid){
        ActionResult actionResult = new ActionResult();
        try {
            List<Business> businesss = businessService.getBusniessByPid(pid);
            actionResult.setCode(200);
            actionResult.setData(businesss);
        }catch (Exception e){
            actionResult.setCode(400);
            actionResult.setHint(e.getMessage());
        }
        return actionResult;
    }
}
